Skip to content

Agent F: Refactor #2+#3 - Consolidate Classifier Methods Mission#26

Merged
Gambitnl merged 3 commits intomainfrom
claude/chat-title-start-011CV4ph2tt6UyVhcYwdPRJy
Nov 12, 2025
Merged

Agent F: Refactor #2+#3 - Consolidate Classifier Methods Mission#26
Gambitnl merged 3 commits intomainfrom
claude/chat-title-start-011CV4ph2tt6UyVhcYwdPRJy

Conversation

@Gambitnl
Copy link
Copy Markdown
Owner

Consolidated duplicate _parse_response() and _build_prompt() methods from OllamaClassifier and GroqClassifier into the BaseClassifier, eliminating ~65 lines of duplicate code.

Changes:

  • Moved _build_prompt() to BaseClassifier with comprehensive docstring

    • Handles prompt template formatting with character/player context
    • Subclasses can override for custom prompt building
  • Moved _parse_response() to BaseClassifier with comprehensive docstring

    • Parses Dutch-format LLM responses (Classificatie/Reden/Vertrouwen/Personage)
    • Handles Classification enum, confidence clamping, and error cases
    • Subclasses can override for different response formats
  • Added preflight_check() to GroqClassifier

    • Validates API key configuration
    • Tests API connectivity
    • Matches OllamaClassifier pattern

Benefits:

  • DRY principle: Single implementation instead of duplicate code
  • Maintainability: Bug fixes and improvements apply to all classifiers
  • Extensibility: New classifiers can inherit default implementations
  • Type safety: Uses Classification enum from constants module

Testing:

  • All 37 existing tests pass
  • Code coverage: 78%
  • No breaking changes - backward compatible

Dependencies:

Refs: Refactor #2+#3 - Consolidate Classifier Methods

Consolidated duplicate _parse_response() and _build_prompt() methods
from OllamaClassifier and GroqClassifier into the BaseClassifier,
eliminating ~65 lines of duplicate code.

Changes:
- Moved _build_prompt() to BaseClassifier with comprehensive docstring
  - Handles prompt template formatting with character/player context
  - Subclasses can override for custom prompt building

- Moved _parse_response() to BaseClassifier with comprehensive docstring
  - Parses Dutch-format LLM responses (Classificatie/Reden/Vertrouwen/Personage)
  - Handles Classification enum, confidence clamping, and error cases
  - Subclasses can override for different response formats

- Added preflight_check() to GroqClassifier
  - Validates API key configuration
  - Tests API connectivity
  - Matches OllamaClassifier pattern

Benefits:
- DRY principle: Single implementation instead of duplicate code
- Maintainability: Bug fixes and improvements apply to all classifiers
- Extensibility: New classifiers can inherit default implementations
- Type safety: Uses Classification enum from constants module

Testing:
- All 37 existing tests pass
- Code coverage: 78%
- No breaking changes - backward compatible

Dependencies:
- Uses Classification enum from src/constants.py (Refactor #7)

Refs: Refactor #2+#3 - Consolidate Classifier Methods
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @Gambitnl, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the classifier architecture by consolidating shared prompt building and response parsing logic into the BaseClassifier. This change adheres to the DRY principle, making the codebase more maintainable and extensible for future classifiers. It also introduces a robust preflight check for the GroqClassifier to ensure proper setup and connectivity before operation.

Highlights

  • Code Consolidation: The _build_prompt() and _parse_response() methods, previously duplicated in OllamaClassifier and GroqClassifier, have been moved to the BaseClassifier. This change eliminates redundant code, centralizes common logic, and includes comprehensive docstrings for both methods.
  • GroqClassifier Preflight Check: A preflight_check() method has been added to GroqClassifier to validate API key configuration and test API connectivity, aligning with the pattern established by OllamaClassifier.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively refactors the classifier classes by moving duplicated _build_prompt and _parse_response methods into the BaseClassifier, which improves maintainability and adheres to the DRY principle. The addition of a preflight_check to GroqClassifier is also a good improvement for consistency and robustness.

My review includes two main points on the new _parse_response method. First, a high-severity concern about the brittleness of the line-by-line parsing logic, which could fail with slightly varied LLM outputs. Second, a medium-severity suggestion to add logging for parsing errors on the confidence value to improve debuggability, instead of failing silently.

Comment thread src/classifier.py Outdated
Comment on lines +138 to +164
lines = response.strip().split('\n')
for line in lines:
line = line.strip()
if line.startswith("Classificatie:"):
class_text = line.split(":", 1)[1].strip().upper()
try:
classification = Classification(class_text)
except ValueError:
self.logger.warning(
"Invalid classification '%s' for segment %s, defaulting to IC",
class_text,
index
)
classification = Classification.IN_CHARACTER
elif line.startswith("Reden:"):
reasoning = line.split(":", 1)[1].strip()
elif line.startswith("Vertrouwen:"):
try:
conf_text = line.split(":", 1)[1].strip()
confidence = float(conf_text)
confidence = ConfidenceDefaults.clamp(confidence)
except ValueError:
pass
elif line.startswith("Personage:"):
char_text = line.split(":", 1)[1].strip()
if char_text.upper() != "N/A":
character = char_text
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current line-by-line parsing approach is brittle and may fail on slightly deviant LLM outputs. For instance:

  • If the reasoning (Reden) is multi-line, any line after the first that starts with a keyword (e.g., Vertrouwen:) will be misinterpreted, truncating the reasoning and potentially corrupting another field.
  • The elif structure correctly handles out-of-order fields, but the multi-line issue is significant as it can lead to silent data corruption.

Consider a more robust parsing strategy. One option is to use a dictionary to first collect all key-value pairs, which would be more resilient to ordering and would make it easier to handle multi-line values for specific keys like Reden.

Comment thread src/classifier.py Outdated
confidence = float(conf_text)
confidence = ConfidenceDefaults.clamp(confidence)
except ValueError:
pass
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Silently passing on a ValueError here can hide issues with the LLM's output for the Vertrouwen field. It's better to log a warning, which provides visibility for debugging while still allowing the fallback to the default confidence value. This would also make it consistent with how invalid Classificatie values are handled.

Suggested change
pass
self.logger.warning(
"Invalid confidence value '%s' for segment %s, using default.",
conf_text,
index
)

Addressed code review feedback from gemini-code-assist:

1. Fixed high-severity multi-line parsing issue:
   - Replaced brittle line-by-line parsing with regex-based extraction
   - Used re.DOTALL flag for reasoning field to handle multi-line values
   - Prevents corruption when field keywords appear in multi-line text
   - Handles out-of-order fields gracefully

2. Fixed medium-severity silent error handling:
   - Added warning log for invalid confidence values
   - Now consistent with classification error handling
   - Improves debuggability by surfacing parsing issues

Changes:
- _parse_response() now uses regex patterns for each field
- Reden (reasoning) captures everything until next field
- More resilient to LLM output variations
- Better error visibility for debugging

Testing:
- All 37 tests still passing
- Backward compatible with existing test cases

Co-authored-by: gemini-code-assist[bot]
Resolved conflicts by keeping improved implementation:
- Regex-based parsing (more robust for multi-line reasoning)
- Enhanced error logging for invalid confidence values
- Better documentation with override guidance

Conflicts resolved in src/classifier.py:
- Kept HEAD version with superior parsing strategy
- Maintained code review improvements
- All 37 tests passing

Changes from main:
- src/pipeline.py updates merged successfully
@Gambitnl Gambitnl merged commit 49ad747 into main Nov 12, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants